package com.framework.wz.wutong.ui.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.framework.wz.wutong.R;
import com.framework.wz.wutong.base.App;
import com.framework.wz.wutong.base.BaseActivity;
import com.framework.wz.wutong.ui.widget.X5WebView;
import com.framework.wz.wutong.utils.PreferenceUtils;
import com.framework.wz.wutong.utils.ToastUtil;
import com.smarx.notchlib.INotchScreen;
import com.smarx.notchlib.NotchScreenManager;
import com.tencent.smtt.export.external.extension.interfaces.IX5WebViewExtension;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;

import butterknife.BindView;

public class H5Activity extends BaseActivity {

    @BindView(R.id.wv)
    X5WebView wv;
    @BindView(R.id.iv_notch)
    ImageView iv_notch;
    private String url = "";

    //刘海屏适配管理器
    private NotchScreenManager notchScreenManager = NotchScreenManager.getInstance();
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_h5;
    }

    @Override
    protected void initView() {
        //获取刘海屏信息
        notchScreenManager.setDisplayInNotch(this);
        notchScreenManager.getNotchInfo(this, new INotchScreen.NotchScreenCallback() {
            @Override
            public void onResult(INotchScreen.NotchScreenInfo notchScreenInfo) {
                if (notchScreenInfo.hasNotch){
                    for (Rect rect : notchScreenInfo.notchRects) {
                        int height = rect.height();
                        Log.i("toolbarHeight","toolbar height is "+height);
                        ViewGroup.LayoutParams params = iv_notch.getLayoutParams();
                        params.height = height;
                        iv_notch.setLayoutParams(params);
                    }
                }
            }
        });
//取消下滑进度小方块
        wv.setHorizontalScrollBarEnabled(false);
        wv.setVerticalScrollBarEnabled(false);
        //下面方法去掉
        IX5WebViewExtension ix5 = wv.getX5WebViewExtension();
        if (null != ix5) {
            ix5.setScrollBarFadingEnabled(false);
        }
        wv.setBackgroundColor(Color.parseColor("#00000000"));
        wv.addJavascriptInterface(new AndroidtoJs(), "wutong");
        initWeb();
    }

    @Override
    protected void initData() {
        url = getIntent().getStringExtra("url");
//        ToastUtil.showLong(H5Activity.this, url);
        Log.e("157", "url = " + url);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    private void initWeb() {

        wv.setWebViewClient(new com.tencent.smtt.sdk.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView view, String url) {
                com.tencent.smtt.sdk.WebView.HitTestResult hitTestResult = view.getHitTestResult();
                //hitTestResult==null解决重定向问题(刷新后不能退出的bug)
                if (!TextUtils.isEmpty(url) && hitTestResult == null) {
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageStarted(com.tencent.smtt.sdk.WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
                showDialog(true);
            }

            @Override
            public void onPageFinished(com.tencent.smtt.sdk.WebView view, String url) {
                super.onPageFinished(view, url);
                showDialog(false);
            }

            @Override
            public void onReceivedError(com.tencent.smtt.sdk.WebView webView, com.tencent.smtt.export.external.interfaces.WebResourceRequest webResourceRequest, com.tencent.smtt.export.external.interfaces.WebResourceError webResourceError) {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
                showDialog(false);
            }

            @Override
            public void onReceivedSslError(com.tencent.smtt.sdk.WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
//                super.onReceivedSslError(webView, sslErrorHandler, sslError);
                sslErrorHandler.proceed();
            }

            @Override
            public void onLoadResource(com.tencent.smtt.sdk.WebView webView, String s) {
                super.onLoadResource(webView, s);
            }

            @Override
            public void onScaleChanged(com.tencent.smtt.sdk.WebView webView, float v, float v1) {
                super.onScaleChanged(webView, v, v1);
            }
        });

        //设置WebChromeClient类
        wv.setWebChromeClient(new com.tencent.smtt.sdk.WebChromeClient() {

            @Override
            public boolean onJsConfirm(com.tencent.smtt.sdk.WebView arg0, String arg1, String arg2,
                                       JsResult arg3) {
                return super.onJsConfirm(arg0, arg1, arg2, arg3);
            }

            /**
             * 全屏播放配置
             */
            @Override
            public void onShowCustomView(View view,
                                         IX5WebChromeClient.CustomViewCallback customViewCallback) {

            }

            @Override
            public void onHideCustomView() {

            }

            @Override
            public boolean onJsAlert(com.tencent.smtt.sdk.WebView arg0, String arg1, String arg2,
                                     JsResult arg3) {
                /**
                 * 这里写入你自定义的window alert
                 */
                return super.onJsAlert(null, arg1, arg2, arg3);
            }

            @Override
            public void onProgressChanged(com.tencent.smtt.sdk.WebView webView, int i) {
                super.onProgressChanged(webView, i);
                if (i > 90) {
                    showDialog(false);
                }
            }
        });

        wv.loadUrl(url);
    }

    public class AndroidtoJs {

    }

}
